package middleware

import (
	"ci-control-backend/common/httpx"
	"ci-control-backend/common/responses"
	"github.com/sirupsen/logrus"
)

// Recover 全局异常拦截器，接收并处理异常，防止gin异常停止
func Recover(ctx *httpx.Context) error {
	defer func() {
		if err := recover(); err != nil {
			logrus.Errorf("panic: %+v", err)
			ctx.Send(responses.ErrorWithMsg("panic: %+v", err))
			ctx.Abort()
		}
	}()
	ctx.Next()
	return nil
}
